//
//  LYChatBaseCell.swift
//  WeChat
//
//  Created by mac on 2018/8/14.
//  Copyright © 2018年 RRC. All rights reserved.
//

import UIKit
import RxSwift
import SnapKit


private let kChatNicknameLabelHeight:CGFloat = 20 //昵称label的高度
let kChatAvatarMarginLeft: CGFloat = 10 //头像的 margin left
let kChatAvatarMarginTop: CGFloat = 0 //头像的 margin top
let kChatAvatarWidth: CGFloat = 40  //头像的宽度

class LYChatBaseCell: UITableViewCell {

    var avatarImageView:UIImageView! {
        didSet{
            avatarImageView.backgroundColor = UIColor.clear
            avatarImageView.width = kChatAvatarWidth
            avatarImageView.height = kChatAvatarWidth
        }
    }
    
    var nicknamelabel:UILabel!{
        didSet{
            nicknamelabel.font = UIFont.systemFont(ofSize: 11)
            nicknamelabel.textColor = UIColor.darkGray
        }
    }
    var model: ChatModel?
    let disposeBag = DisposeBag()
    
    override func prepareForReuse() {
        self.avatarImageView.image = nil
        self.nicknamelabel.text = nil
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.selectionStyle = .none
        self.contentView.backgroundColor = UIColor.clear
        self.backgroundColor = UIColor.clear
        
        self.avatarImageView = UIImageView(frame: CGRect(x: 10, y: 0, width: 45, height: 45))
        self.contentView.addSubview(self.avatarImageView)
        
        self.nicknamelabel = UILabel(frame: CGRect(x: 62, y: 2, width: 195, height: 21))
        self.contentView.addSubview(self.nicknamelabel)
        
        //点像点击
        let tap = UITapGestureRecognizer()
        self.avatarImageView.addGestureRecognizer(tap)
        self.avatarImageView.isUserInteractionEnabled = true
        tap.rx.event.subscribe { (tap) in
            print("点了头像")
            }.disposed(by: self.disposeBag)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func setNeedsDisplay() {
        super.setNeedsDisplay()
        
    }
    
    func setCellContent(_ model:ChatModel) {
        self.model = model
        if self.model!.fromMe {
            let avatarURL = "http://ww3.sinaimg.cn/thumbnail/6a011e49jw1f1e87gcr14j20ks0ksdgr.jpg"
            self.avatarImageView.ly_setImageWithURLString(avatarURL, placeholderImage: LYAsset.Icon_avatar.image)
        }else{
            let avatarURL = "http://ww2.sinaimg.cn/large/6a011e49jw1f1j01nj8g6j204f04ft8r.jpg"
            self.avatarImageView.ly_setImageWithURLString(avatarURL, placeholderImage: LYAsset.Icon_avatar.image)
        }
        
        self.setNeedsLayout()
    }
    
    override func layoutSubviews() {
        guard let model = self.model else {
            return
        }
        if model.fromMe {
            self.nicknamelabel.height = 0
            self.avatarImageView.left = UIScreen.ts_width - kChatAvatarMarginLeft - kChatAvatarWidth
        }else{
            self.nicknamelabel.height = 0
            self.avatarImageView.left = kChatAvatarMarginLeft
        }
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
